package com.centaline.androidsalesblog.fragments.list;

import android.content.Intent;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.sale.RentDetailActivity;
import com.centaline.androidsalesblog.adapter.RentListAdapter;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.trello.rxlifecycle.FragmentEvent;

import java.util.ArrayList;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 租房
 * Created by guilin on 16/1/26.
 */
public class RentListFragment extends BaseFragment {


    private MRecyclerView mRecyclerView;

    private RentListAdapter rentListAdapter;
    private ArrayList<PostDo> list = new ArrayList<>();
    private ArrayList<String> postIds = new ArrayList<>();
    private int iRefreshType = 1;
    private RefreshListener refreshListener;
    private IRecycleViewListener iRecycleViewListener = new IRecycleViewListener() {
        @Override
        public void downRefresh() {
            iRefreshType = 1;
            refreshListener.down();
        }

        @Override
        public void upRefresh() {
            iRefreshType = 2;
            refreshListener.up(list.size());
        }

        @Override
        public void onItemClick(int position) {
            startActivity(new Intent(getActivity(), RentDetailActivity.class)
                    .putExtra(ExtraConstant.PostId, list.get(position).getPostId()));
            if (!postIds.contains(list.get(position).getPostId())) {
                postIds.add(list.get(position).getPostId());
                saveBrowseId(list.get(position).getPostId());
            }
            mRecyclerView.notifyItemChanged(position);
        }
    };

    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.layout_m_recyclerview;
    }

    @Override
    protected void findViews(View view) {
        mRecyclerView = (MRecyclerView) view.findViewById(R.id.mRecyclerView);
        mRecyclerView.startRefresh();
    }

    @Override
    protected void initView() {
        mRecyclerView.setIRecycleViewListener(iRecycleViewListener);
        rentListAdapter = new RentListAdapter(getActivity(), GlideLoad.init(this), list, postIds);
        mRecyclerView.setAdapter(rentListAdapter);
        getBrowse();
        startRefresh();
    }

    /**
     * 开启下拉刷新
     */
    public void startRefresh() {
        mRecyclerView.startRefresh();
    }

    /**
     * 设置定位信息
     */
    public void setLocation(LatLng latLng) {
        rentListAdapter.setLocation(latLng);
    }

    /**
     * 停止刷新
     */
    public void stopRefresh(ArrayList<PostDo> arrayList) {
        if (iRefreshType == 1) {
            list.clear();
        }
        if (arrayList == null) {
            mRecyclerView.stopRefresh(false);
        } else {
            list.addAll(arrayList);
            mRecyclerView.stopRefresh(arrayList.size() == 10);
        }
    }

    /**
     * 刷新失败
     */
    public void refreshError() {
        mRecyclerView.refreshError();
    }

    public void getBrowse() {
        RxDataBase.getBrowseIds(1)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<String>>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<ArrayList<String>>() {
                    @Override
                    public void call(ArrayList<String> strings) {
                        postIds.clear();
                        postIds.addAll(strings);
                    }
                });
    }

    public void saveBrowseId(String id) {
        RxDataBase.saveBrowse(1, id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {

                    }
                });
    }
}
